using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web.Core;
using Web.Core.Objects;

public partial class UserControls_Pager : System.Web.UI.UserControl
{    
    public int totalObjects;
    public int objectsPerPage = 2;
    public string objectsName = "objects";
    public bool displayAll = true;
    int currentPage;
    int totalPage;   
    string url;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (totalObjects <= 0)
            return;
        totalPage = GetTotalPage();
        url = Utils.HandleURL(Request.Url.PathAndQuery, "page");
        if (totalPage == 0)
            return;
        else
        {
            currentPage = GetCurrentPage();
            if (displayAll == true)
                this.lblLeft.Text = HandleDisplaying(currentPage, totalObjects);
            this.lblRight.Text = HandlePages(currentPage);
        }
    }

    public int GetCurrentPage()
    {
        int curr = 1;
        if (Request.Params["page"] != null)
            curr = int.Parse(Request.Params["page"]);
        return curr;
    }

    public int GetTotalPage()
    {
        int result = 0;
        if (totalObjects % objectsPerPage == 0)
            result = totalObjects / objectsPerPage;
        else
            result = totalObjects / objectsPerPage + 1;
        return result;
    }

    public string HandlePages(int page) // Hien thi ben phai
    {
        string result = Resources.Lang.ResultPages + ": ";

        int next = page + 3;
        int previous = page - 3;
        if (next > totalPage)
            next = totalPage;
        if (previous < 1)
            previous = 1;

        if (page > 1)
        {
            result += "<a href=\"" + url + "page=" + previous + "\">[<]</a> ";
            page--;
            result += "<a href=\"" + url + "page=" + page + "\">" + page + "</a> ";
            page++;
            result += "<span class=\"pagerCurrentPage\">" + page + "</span> ";
            if (page < totalPage)
            {
                page++;
                result += "<a href=\"" + url + "page=" + page + "\">" + page + "</a> ";
            }
            if (currentPage + 1 < totalPage)
                result += "<span class=\"pagerCurrentPage\">. . .</span> ";
            result += "<a href=\"" + url + "page=" + next + "\">[>]</a> ";
        }
        else if (page == 1)
        {
            result += "<span class=\"pagerCurrentPage\">" + page + "</span> ";
            page++;
            if (page < totalPage)
            {
                result += "<a href=\"" + url + "page=" + page + "\">" + page + "</a> ";
                page++;
            }
            if (page < totalPage + 1)
            {
                result += "<a href=\"" + url + "page=" + page + "\">" + page + "</a> ";
                if (currentPage + 2 < totalPage)
                    result += "<span class=\"pagerCurrentPage\">. . .</span> ";
            }
            result += "<a href=\"" + url + "page=" + next + "\">[>]</a> ";
        }
        return result;
    }

    public string HandleDisplaying(int curr, int totalPro) // Hien thi ben trai
    {
        int from = 0, to = 0;
        from = ((currentPage - 1) * objectsPerPage + 1);
        to = (currentPage * objectsPerPage);
        if (to > totalPro)
            to = totalPro;
        return Resources.Lang.Displaying + " " + from + " " + Resources.Lang.to + " " + to + " ( " + Resources.Lang.of + " " + totalObjects + " " + objectsName + " )";
    }
}
